home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_019 / blackjack / ttymgr.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  117 lines

  1. /* TTYMGR      - tty (terminal) manager
  2.  */
  3.  
  4. #include "local.h"
  5. #include "bj.h"
  6. #include "hndmgr.h"
  7. #include "ttymgr.h"
  8.  
  9. #define NMSGS 4
  10. #define LENMSG 15
  11.  
  12. static bool askedhit = NO;
  13. static bool wanthit = NO;
  14. static char qchar[NMSGS+1] = "dsih";
  15. static char qmsg[NMSGS][LENMSG+1] =
  16.    {  "Double down",
  17.       "Split pair",
  18.       "Insurance",
  19.       "Hit",
  20.    };
  21. static short nmsg[NMSGS] = {0};
  22.  
  23. /* query  -  get player's response for DBLDN, SPLIT, HIT
  24.  */
  25. short query()
  26. {
  27.    short ask(), val();
  28.    short ret;        /* return from ask() */
  29.  
  30.    if (val(1, 0) == val(1, 1))
  31.       ret = ask("dsh");
  32.    else
  33.       ret = ask("dh");
  34.    askedhit = (ret != SPLIT);
  35.    wanthit = (ret == HIT);
  36.    if (wanthit)
  37.       ret = NONE;
  38.    return (ret);
  39. }
  40.  
  41. /* takes    - gets a YES or NO reply to question
  42.  */
  43. bool takes(s)
  44.    char s[];
  45. {
  46.    short ask();
  47.    int strcmp();
  48.  
  49.    if (askedhit && strcmp(s, "h") == 0) {
  50.       askedhit = NO;
  51.       return(wanthit);
  52.    }
  53.    return (ask(s) != NONE);
  54. }
  55.  
  56. /* ask      - gets a choice among alternatives
  57.  */
  58. static short ask(s)
  59.    char s[];
  60. {
  61.    bool  isbrief;             /* is prompt brief? */
  62.    char ans[BUFSIZ];          /* player's reply line */
  63.    char c;                    /* player's one-char answer */
  64.    short i;                   /* index over chars of s */
  65.    short j;                   /* index over chars of qchar */
  66.    short slen;                /* length of s */
  67.    static short msglim = 5;   /* verbosity limit */
  68.  
  69.    unsigned strscn();         /* gives the index of c in s */
  70.    int   strlen();
  71.    void  error();
  72.  
  73.    isbrief = YES;
  74.    slen = strlen(s);
  75.    for (i = 0; i < slen; ++i) {
  76.       j = strscn(qchar, s[i]);
  77.       if (++nmsg[j] <= msglim)
  78.          isbrief = NO;
  79.    }
  80.    if (isbrief) {
  81.       for (i = 0; i < slen; ++i)
  82.          printf("%c?", s[i]);
  83.       printf("\n");
  84.    }
  85.    FOREVER {
  86.       if (!isbrief) {
  87.          printf("Type\n");
  88.          for (i = 0; i < slen; ++i)
  89.             printf("%c      For %s\n", s[i], qmsg[strscn(qchar, s[i])]);
  90.          printf("RETURN for None\n");
  91.       }
  92.       if (getln(ans, BUFSIZ) == EOF)
  93.          error("Bye!", "");
  94.       c = tolower(ans[0]);
  95.       if (c == '\n')
  96.          return(NONE);
  97.       for (i = 0; i < slen; ++i)
  98.          if (s[i] == c)
  99.             return ((short)(1 + strscn(qchar, c)));
  100.       isbrief = NO;
  101.    }
  102. }
  103.  
  104. /* strscn      - return the index of c in string s
  105.  */
  106. unsigned strscn(s, c)
  107.    char s[];      /* string to be scanned */
  108.    char c;        /* char to be matched */
  109. {
  110.    register unsigned i;
  111.  
  112.    for (i = 0; s[i] != c && s[i] != '\0'; ++i)
  113.       ;
  114.    return(i);
  115. }
  116.  
  117.